//: C18:Datascan.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
//{L} Datalog
// Verify and view logged data
#include "DataLogger.h"
#include "../require.h"
#include <iostream>
#include <fstream>
#include <strstream>
#include <iomanip>
using namespace std;

int main()
{
    ifstream bindata("data.bin", ios::binary);
    assure(bindata, "data.bin");
    // Create comparison file to verify data.txt:
    ofstream verify("data2.txt");
    assure(verify, "data2.txt");
    DataPoint d;
    while (bindata.read(
                (unsigned char*)&d, sizeof d))
        d.print(verify);
    bindata.clear(); // Reset state to "good"
    // Display user-selected records:
    int recnum = 0;
    // Left-align everything:
    cout.setf(ios::left, ios::adjustfield);
    // Fixed precision of 4 decimal places:
    cout.setf(ios::fixed, ios::floatfield);
    cout.precision(4);
    for (;;) {
        bindata.seekg(recnum* sizeof d, ios::beg);
        cout << "record " << recnum << endl;
        if (bindata.read(
                    (unsigned char*)&d, sizeof d)) {
            cout << asctime(&(d.getTime()));
            cout << setw(11) << "Latitude"
                 << setw(11) << "Longitude"
                 << setw(10) << "Depth"
                 << setw(12) << "Temperature"
                 << endl;
            // Put a line after the description:
            cout << setfill('-') << setw(43) << '-'
                 << setfill(' ') << endl;
            cout << setw(11) << d.getLatitude()
                 << setw(11) << d.getLongitude()
                 << setw(10) << d.getDepth()
                 << setw(12) << d.getTemperature()
                 << endl;
        } else {
            cout << "invalid record number" << endl;
            bindata.clear(); // Reset state to "good"
        }
        cout << endl
             << "enter record number, x to quit:";
        char buf[10];
        cin.getline(buf, 10);
        if (buf[0] == 'x') break;
        istrstream input(buf, 10);
        input >> recnum;
    }
} ///:~
